package org.bigmarketapi.gatewaycenter.trigger.http;

import org.bigmarketapi.gatewaycenter.application.IGatewayConfig;
import org.bigmarketapi.gatewaycenter.domain.manage.service.IManageService;
import org.bigmarketapi.gatewaycenter.infrastructure.persistent.common.ResponseCode;
import org.bigmarketapi.gatewaycenter.infrastructure.persistent.common.Result;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @BelongsProject: Gateway-Center
 * @BelongsPackage: org.bigmarketapi.gatewaycenter.trigger.http
 * @Author: dujiaqi
 * @CreateTime: 2024-12-08  14:53
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/wg/admin/registerGateway/")
public class GatewayConfigManage implements IGatewayConfig {
    IManageService manageService;

    public GatewayConfigManage(IManageService manageService) {
        this.manageService = manageService;
    }

    @Override
    @PostMapping(value = "registerGatewayServerNode", produces = "application/json;charset=utf-8")
    public Result<Boolean> registerGatewayServerNode(@RequestParam String groupId, @RequestParam String gatewayId, @RequestParam String gatewayName, @RequestParam String gatewayAddress) {
        try {
            final boolean registered = manageService.registerGatewayServerNode(groupId, gatewayId, gatewayName, gatewayAddress);
            return Result.<Boolean>builder().code(ResponseCode.SUCCESS.getCode()).info(ResponseCode.SUCCESS.getInfo()).value(registered).build();
        } catch (DuplicateKeyException e) {
            return Result.<Boolean>builder().code(ResponseCode.INDEX_DUP.getCode()).info(e.getMessage()).value(false).build();
        }
    }
}
